#include "windowWidget.h"
#include <QWidget>
#include <QTreeView>
#include <QPushButton>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QMessageBox>

#include "ui_windowWidget.h"

windowWidget::windowWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::windowWidget)
{
    ui->setupUi(this);

    model = new QFileSystemModel(this);
    model->setRootPath(QDir::currentPath());

    ui->treeView->setModel(model);
    ui->treeView->setRootIndex(model->index(QDir::currentPath()));

    setWindowTitle("File System Model");

    connect(ui->mkdir, SIGNAL(clicked()),this, SLOT(mkdir()));
    connect(ui->remove, SIGNAL(clicked()),this, SLOT(rm()));
    ui->treeView->expandAll();
}

windowWidget::~windowWidget()
{
    delete ui;
}

void windowWidget::setPowerLevel(int pow)
{
    m_state = pow;
}

int windowWidget::getPowerLevel()
{
    return m_state;
}

void windowWidget::mkdir()
{
    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) {
        return;
    }
    QString dirName = QInputDialog::getText(this,
                                            tr("Create Directory"),
                                            tr("Directory name"));
    if (!dirName.isEmpty()) {
        if (!model->mkdir(index, dirName).isValid()) {
            QMessageBox::information(this,
                                     tr("Create Directory"),
                                     tr("Failed to create the directory"));
        }
    }
}

void windowWidget::rm()
{
    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) {
        return;
    }
    bool ok;
    if (model->fileInfo(index).isDir()) {
        ok = model->rmdir(index);
    } else {
        ok = model->remove(index);
    }
    if (!ok) {
        QMessageBox::information(this,
                         tr("Remove"),
                         tr("Failed to remove %1").arg(model->fileName(index)));
    }
}

